package middleware

import (
	"github.com/zeromicro/go-zero/rest/httpx"
	"net/http"
	"taobao-merchandis/api/internal/types"
	"taobao-merchandis/pkg"
)

type UserAgentMiddleware struct {
}

func NewUserAgentMiddleware() *UserAgentMiddleware {
	return &UserAgentMiddleware{}
}

func (m *UserAgentMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		// TODO generate middleware implement function, delete after code implementation
		token := r.Header.Get("token")
		GetToKen, s := pkg.GetToKen(token)
		if s != "" {
			response := types.Response{
				Message: s,
				Data:    nil,
				Code:    500,
			}
			httpx.OkJsonCtx(r.Context(), w, response)
		}
		r.ParseForm()
		r.Form.Set("userId", GetToKen["userId"].(string))
		// Passthrough to next handler if need
		next(w, r)
	}
}
